package com.lau.videoeffect.multiplayer

import android.content.Context
import android.opengl.GLES20
import android.opengl.GLSurfaceView
import javax.microedition.khronos.egl.EGLConfig
import javax.microedition.khronos.opengles.GL10

class GLRenderer : GLSurfaceView.Renderer {

    private val mVideoRenderedList = ArrayList<IVideoRenderer>()

    constructor(context: Context, path1: String, path2: String) {
        val renderer1 = VideoRenderer(context, path1)
        val renderer2 = MovingTextureRenderer(context, path2)
        renderer2.setVideoRenderer(renderer1)
        mVideoRenderedList.add(renderer1)
        mVideoRenderedList.add(renderer2)
    }

    override fun onDrawFrame(p0: GL10?) {
        GLES20.glClear(GLES20.GL_DEPTH_BUFFER_BIT xor GLES20.GL_COLOR_BUFFER_BIT)
        GLES20.glEnable(GLES20.GL_BLEND)
        GLES20.glBlendFunc(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA)
        for (rendered in mVideoRenderedList) {
            rendered.onDraw()
        }
    }

    override fun onSurfaceChanged(p0: GL10?, p1: Int, p2: Int) {
        for (rendered in mVideoRenderedList) {
            rendered.onSurfaceChanged(p1, p2)
        }
    }

    override fun onSurfaceCreated(p0: GL10?, p1: EGLConfig?) {
        for (rendered in mVideoRenderedList) {
            rendered.onSurfaceCreated()
        }
    }

    fun onResume() {
        for (rendered in mVideoRenderedList) {
            rendered.onResume()
        }
    }

    fun onPause() {
        for (rendered in mVideoRenderedList) {
            rendered.onPause()
        }
    }

    fun onRelease() {
        for (rendered in mVideoRenderedList) {
            rendered.onRelease()
        }
    }

}